package com.itheima.test;

import com.itheima.domain.Product;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class MyTset {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
    List<Product> list =new ArrayList<>();
    Product product =new Product();

    /**
     * @throws Exception
     * 查询所有
     */
    @Test
    public void findAll() throws Exception{
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SQLSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.执行映射配置文件中的sql语句，并接受结果
        List<Product> list = sqlSession.selectList("product.findAll");
        //5.输出结果
        for (Product product : list) {
            System.out.println(product);
        }
        //6.释放资源
        sqlSession.close();
        is.close();
    }

    /**
     * @param id
     * 根据id查询单个
     */
    @Test
    public void findById() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Integer id = 2;
        Product product = sqlSession.selectOne("product.findById", id);
        System.out.println(product);
        sqlSession.close();
        is.close();
    }

    /**
     * @param pro
     * 根据id添加单个
     */
    @Test
    public void add() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Product product = new Product("iPad",1999.0,sdf.format(new Date()),"数码产品");
        int result = sqlSession.insert("product.add", product);
        System.out.println(result);
        sqlSession.close();
        is.close();
    }

    /**
     * 根据id修改
     */
    @Test
    public void updateById() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Integer id =3;
        Product product = sqlSession.selectOne("product.findById",id);
        product.setName("Lv");
        product.setPrice(13888.0);
        product.setCategory("鞋靴箱包");
        int result = sqlSession.update("product.updateById", product);
        System.out.println(result);
        sqlSession.close();
        is.close();
    }

    /**
     * 根据id删除 物理删除，恢复不了
     */
    @Test
    public void deleteById() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Integer id =3;
        int result = sqlSession.delete("product.deleteById", id);
        System.out.println(result);
        sqlSession.close();
        is.close();
    }

    /**
     * 根据id删除  状态删除
     */
    @Test
    public void deleteByIdStatus() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        Integer id =2;
        int result = sqlSession.delete("product.deleteByIdStatus", id);
        System.out.println(result);
        sqlSession.close();
        is.close();
    }
    /**
     * @param pro
     * 通过姓名、入库时间和价格任意条件查询
     */
    @Test
    public void findByNPT() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        product.setName("oppo手机");
       // product.setPrice(2999.0);
       // product.setInsertTime("2020-07-01" );
         list = sqlSession.selectList("product.findByNPT", product);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(1));
        }
       // System.out.println(list);


        sqlSession.close();
        is.close();
    }

    /**
     * @param pro
     * 修改指定列 name price
     */
    @Test
    public void updateByNameAndPrice() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
         product.setId(6);
        product.setName("oppo手机");
         product.setPrice(3499.0);
        int result = sqlSession.update("product.updateByNameAndPrice", product);
        System.out.println(result);
        sqlSession.close();
        is.close();
    }

    /**
     * @param list
     * 批量增加
     */
    @Test
    public void addBatch() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
       list.add(new Product(null,"oppo手机",3888.0,"2020-05-30" ,"数码产品"));
        list.add(new Product(null,"魅族",2888.0,"2020-06-16" ,"数码产品"));
        list.add(new Product(null,"小米电脑",6888.0,"2020-06-26" ,"数码产品"));
        for (int i = 0; i <100 ; i++) {
            list.add(new Product(null,"华为手机"+i,5999.0+i,sdf.format(new Date()),"数码产品"));
        }
        int result = sqlSession.insert("product.addBatch", list);
        System.out.println(result);
        sqlSession.close();
        is.close();

    }
}
